package org.crumbleworks.crumbchat.server.gui;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import org.crumbleworks.crumbchat.server.controller.GuiController;

public class ChatPanel extends JPanel implements ActionListener {
    private static final long serialVersionUID = 1L;

    private JTextArea messageArea;
    private DefaultListModel<String> usernames;
    private JList<String> userList;
    private JTextField messageField;
    private JButton sendButton;

    private GuiController controller;

    public ChatPanel(GuiController controller) {
        this.controller = controller;

        setLayout(new GridBagLayout());

        initComponents();
        placeComponents();

        setVisible(true);
    }

    private void initComponents() {
        messageArea = new JTextArea(15, 30);
        messageArea.setEditable(false);
        messageArea.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));

        usernames = new DefaultListModel<String>();

        userList = new JList<String>(usernames);
        userList.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));

        messageField = new JTextField(20);
        messageField.addActionListener(this);

        sendButton = new JButton("send");
        sendButton.addActionListener(this);
    }

    private void placeComponents() {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(5, 5, 5, 5);

        // MessageArea
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        add(messageArea, gbc);

        // UserList
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        add(userList, gbc);

        // MessageField
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        add(messageField, gbc);

        // SendButton
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        add(sendButton, gbc);
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        if (ae.getSource().equals(sendButton) && !messageField.getText().isEmpty() || ae.getSource().equals(messageField)
                && !messageField.getText().isEmpty()) {
            controller.parseInput(messageField.getText());
            messageField.setText("");
        }
    }

    public void putMessage(String username, String message) {
        messageArea.append(username + ": " + message + "\n");
    }

    public void putUsernames(ArrayList<String> usernames) {
        for (String username : usernames) {
            this.usernames.addElement(username);
        }
    }
}
